package com.example.keli.ui.reward;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.util.Log;

import com.example.keli.R;
import com.example.keli.api.RetrofitManager;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;
import com.example.keli.base.BaseFragment;
import com.example.keli.base.utils.DensityUtil;
import com.example.keli.base.utils.LogUtils;
import com.kennyc.view.MultiStateView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * description 获奖名单
 * created by jerry on 2019/3/18.
 */
public class RewardListFragment extends BaseFragment {

    private static final String TAG = "RewardListFragment";
    @BindView(R.id.refreshlayout)
    SwipeRefreshLayout refreshlayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.multiStateView)
    MultiStateView multiStateView;

    private RewardListAdapter rewardListAdapter;
    private List<RewardListVO.DataBean> mListData;
    private int page = 1;

    public static RewardListFragment newInstance() {
        RewardListFragment rewardObjectFragment = new RewardListFragment();
        Bundle bundle = new Bundle();
        rewardObjectFragment.setArguments(bundle);
        return rewardObjectFragment;
    }

    @Override
    protected int layoutId() {
        return R.layout.fragment_rewardlist;
    }

    @Override
    protected void bindData() {
        initRecycleView();
        loadDatas();
    }

    @Override
    protected void bindListener() {
        refreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mListData.clear();
                loadDatas();
            }
        });
    }

    private void initRecycleView() {
        mListData = new ArrayList<>();
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        rewardListAdapter = new RewardListAdapter(mListData, getContext());
        recyclerView.setAdapter(rewardListAdapter);
        multiStateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
    }

    private void loadDatas() {
        Map<String, String> map = new HashMap<>();
        map.put("page", String.valueOf(page));
        RetrofitManager.ApiFunc(RetrofitManager.mApiService.getAnnouncement(map), new SubscriberListener<BaseResponse<RewardListVO>>() {
            @Override
            public void onSuccess(BaseResponse<RewardListVO> rewardListBaseResponse) {
                List<RewardListVO.DataBean> data = rewardListBaseResponse.data.getData();
                if (page == 1) {
                    refreshlayout.setRefreshing(false);
                    if(data.size()==0){
                        multiStateView.setViewState(MultiStateView.VIEW_STATE_EMPTY);
                    }else{
                        multiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                    }
                }
                if (data.size() == 0) {
                    rewardListAdapter.loadMoreEnd();
                    return;
                }
                mListData.addAll(data);
                rewardListAdapter.notifyDataSetChanged();
                rewardListAdapter.loadMoreComplete();
            }

            @Override
            public void onFailer(String msg) {
                refreshlayout.setRefreshing(false);
            }

            @Override
            public void onTokenError() {
                refreshlayout.setRefreshing(false);
            }
        });
    }
}
